﻿using BCCommon;
using BCEntity.OA.BusinessFlowCategory;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;

namespace BCData.OA.BusinessFlowCategory
{
    /// <summary>
    /// OA系统业务流分类
    /// </summary>
	public class BusinessFlowCategoryData : IBusinessFlowCategoryData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM OA_BusinessFlowCategory";
        private const string QUERY = @"SELECT BusinessFlowCategoryId,EditTime,BusinessFlowCategoryName,CompanyId,IsPredefined,CreateAdminId,CreateAdminName,CreateTime,EditAdminId,EditAdminName 
                                        FROM OA_BusinessFlowCategory";
        public BusinessFlowCategoryData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        public async Task<bool> Exists(long businessFlowCategoryId)
        {
            string sql = string.Format(@"{0} WHERE BusinessFlowCategoryId=?BusinessFlowCategoryId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BusinessFlowCategoryId", businessFlowCategoryId, DbType.Int64);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }

        public async Task<bool> Exists(string businessFlowCategoryName)
        {
            string sql = string.Format(@"{0} WHERE BusinessFlowCategoryName=?BusinessFlowCategoryName", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BusinessFlowCategoryName", businessFlowCategoryName?.Trim(), DbType.AnsiString);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }

        public async Task<BusinessFlowCategoryEntity> Add(BusinessFlowCategoryEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("OA_BusinessFlowCategory", new string[] { "BusinessFlowCategoryName", "CompanyId", "IsPredefined", "CreateAdminId", "CreateAdminName", "" });
            sql = string.Format("{0} {1} WHERE BusinessFlowCategoryId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BusinessFlowCategoryName", entity.BusinessFlowCategoryName?.Trim(), DbType.AnsiString, size: 30);
            parameters.Add("?CompanyId", entity.CompanyId, DbType.Int64, size: 20);
            parameters.Add("?IsPredefined", entity.IsPredefined, DbType.Boolean);
            parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
            parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<BusinessFlowCategoryEntity>(sql, parameters, databaseContext.Transaction);

        }

        public async Task<BusinessFlowCategoryEntity> Update(BusinessFlowCategoryEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("OA_BusinessFlowCategory", new string[] { "EditTime", "BusinessFlowCategoryName", "CompanyId", "IsPredefined", "CreateAdminId", "CreateAdminName", "EditAdminId", "EditAdminName" }, new string[] { "BusinessFlowCategoryId" });
            sql = string.Format("{0} {1} WHERE BusinessFlowCategoryId=?BusinessFlowCategoryId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BusinessFlowCategoryId", entity.BusinessFlowCategoryId, DbType.Int64, size: 20);
            parameters.Add("?EditTime", entity.EditTime, DbType.DateTime);
            parameters.Add("?BusinessFlowCategoryName", entity.BusinessFlowCategoryName?.Trim(), DbType.AnsiString, size: 30);
            parameters.Add("?CompanyId", entity.CompanyId, DbType.Int64, size: 20);
            parameters.Add("?IsPredefined", entity.IsPredefined, DbType.Boolean);
            parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
            parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
            parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<BusinessFlowCategoryEntity>(sql, parameters, databaseContext.Transaction);
        }

        public async Task<bool> Delete(long businessFlowCategoryId)
        {
            string sql = @"DELETE FROM OA_BusinessFlowCategory WHERE BusinessFlowCategoryId=?BusinessFlowCategoryId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BusinessFlowCategoryId", businessFlowCategoryId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<BusinessFlowCategoryEntity> GetEntity(long businessFlowCategoryId)
        {
            string sql = string.Format(@"{0} WHERE BusinessFlowCategoryId=?BusinessFlowCategoryId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BusinessFlowCategoryId", businessFlowCategoryId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<BusinessFlowCategoryEntity>(sql, parameters);
        }

        public async Task<IEnumerable<BusinessFlowCategoryEntity>> GetList(int companyId)
        {
            string sql = string.Format("{0} WHERE CompanyId=?CompanyId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?CompanyId", companyId, DbType.Int32);
            return await this.databaseContext.Slave.QueryAsync<BusinessFlowCategoryEntity>(sql, parameters);
        }

        public async Task<(IEnumerable<BusinessFlowCategoryEntity>, int)> Query(int companyId, string businessFlowCategoryName, DateTime? start, DateTime? end, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            wheres.Add("CompanyId=?CompanyId");
            parameters.Add("?CompanyId", companyId, DbType.Int32);
            if (!string.IsNullOrWhiteSpace(businessFlowCategoryName))
            {
                wheres.Add("BusinessFlowCategoryName=?BusinessFlowCategoryName");
                parameters.Add("?BusinessFlowCategoryName", businessFlowCategoryName?.Trim(), DbType.AnsiString);
            }
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY CreateTime DESC LIMIT {2},{3}; SELECT COUNT(1) FROM OA_BusinessFlowCategory {1}",
                   QUERY,
                   wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                   (pageIndex - 1) * pageSize,
                   pageSize);
            return await this.databaseContext.Slave.QueryPageableListAsync<BusinessFlowCategoryEntity>(sql, parameters);
        }
    }
}
